package myJs.implicits

import japgolly.scalajs.react.Reusability
import japgolly.scalajs.react.Reusability.byRef

import scala.collection.immutable.SeqMap
import myJs.myPkg.jquery._

/**
 * Created by Administrator on 2020/1/16
 */
trait MyReactTool {

  def rApply[A](f: (A, A) => Boolean): Reusability[A] =
    new Reusability(f)

  implicit def rSeqMap[K, V](implicit rv: Reusability[V]): Reusability[SeqMap[K, V]] = {
    byRef[SeqMap[K, V]] || rApply((m: SeqMap[K, V], n: SeqMap[K, V]) => {
      if (m.isEmpty)
        n.isEmpty
      else if (n.isEmpty)
        false
      else {
        var ok = true
        var msize = 0

        val mi = m.iterator
        while (ok && mi.hasNext) {
          val (k, v) = mi.next()
          msize += 1
          ok = n.get(k).exists(rv.test(v, _))
        }

        ok && msize == n.size
      }
    })
  }

  implicit def rJq: Reusability[JQuery] =
    rApply((x, y) => x == y)


}
